Solves #357 #359

Glenn 'devalias' Grant 10 years ago
parent
commit
370297ced3
1 changed files with 37 additions and 13 deletions
  1. 37 13
      app/models/agents/twitter_user_agent.rb

+ 37 - 13
app/models/agents/twitter_user_agent.rb

@@ -17,11 +17,15 @@ module Agents
17 17
 
18 18
       You must also provide the `username` of the Twitter user to monitor.
19 19
 
20
+      Set `include_retweets` to `false` to not include retweets (default: `true`)
21
+
20 22
       Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
23
+
24
+      Set `starting_at` to the date/time (eg. `Mon Jun 02 00:38:12 +0000 2014`) you want to start receiving tweets from (default: agent's `created_at`)
21 25
     MD
22 26
 
23 27
     event_description <<-MD
24
-      Events are the raw JSON provided by the Twitter API. Should look something like:
28
+      Events are the raw JSON provided by the [Twitter API](https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline). Should look something like:
25 29
 
26 30
           {
27 31
              ... every Tweet field, including ...
@@ -46,38 +50,58 @@ module Agents
46 50
 
47 51
     default_schedule "every_1h"
48 52
 
53
+    def working?
54
+      event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
55
+    end
56
+
57
+    def default_options
58
+      {
59
+        'username' => 'tectonic',
60
+        'include_retweets' => 'true',
61
+        'expected_update_period_in_days' => '2'
62
+      }
63
+    end
64
+
49 65
     def validate_options
50 66
       unless options['username'].present? &&
51 67
         options['expected_update_period_in_days'].present?
52 68
         errors.add(:base, "username and expected_update_period_in_days are required")
53
-      end      
69
+      end
54 70
     end
55 71
 
56
-    def working?
57
-      event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs?
72
+    def starting_at
73
+      if options[:starting_at].present?
74
+        Time.parse(options[:starting_at])
75
+      else
76
+        created_at
77
+      end
58 78
     end
59 79
 
60
-    def default_options
61
-      {
62
-        'username' => "tectonic",
63
-        'expected_update_period_in_days' => "2"
64
-      }
80
+    def include_retweets?
81
+      if options[:include_retweets].present?
82
+        !!options[:include_retweets]
83
+      else
84
+        true
85
+      end
65 86
     end
66 87
 
67 88
     def check
68 89
       since_id = memory['since_id'] || nil
69
-      opts = {:count => 200, :include_rts => true, :exclude_replies => false, :include_entities => true, :contributor_details => true}
90
+      opts = {:count => 200, :include_rts => include_retweets?, :exclude_replies => false, :include_entities => true, :contributor_details => true}
70 91
       opts.merge! :since_id => since_id unless since_id.nil?
71 92
 
93
+      # http://rdoc.info/gems/twitter/Twitter/REST/Timelines#user_timeline-instance_method
72 94
       tweets = twitter.user_timeline(options['username'], opts)
73 95
 
74 96
       tweets.each do |tweet|
75
-        memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
97
+        if tweet.created_at >= starting_at
98
+          memory['since_id'] = tweet.id if !memory['since_id'] || (tweet.id > memory['since_id'])
76 99
 
77
-        create_event :payload => tweet.attrs
100
+          create_event :payload => tweet.attrs
101
+        end
78 102
       end
79 103
 
80 104
       save!
81 105
     end
82 106
   end
83
-end
107
+end